import { DataTypes } from 'sequelize'
import {
  Table,
  Column,
  Model,
  Comment,
  AllowNull,
  BelongsTo,
  ForeignKey,
  Default,
  AutoIncrement,
  PrimaryKey
} from 'sequelize-typescript'
import { InferAttributes, InferCreationAttributes } from 'sequelize/types'
import Blog from './Blog'
import User from './User'
//防止与装饰器@Comment起冲突所以取名Comments
@Table({
  //表名不自动添加s
  freezeTableName: true
})
class Comments extends Model<InferAttributes<Comments>, InferCreationAttributes<Comments>> {
  @PrimaryKey
  @AutoIncrement
  @Column
  comment_id?: number

  @Comment('博客id')
  @AllowNull(false)
  @ForeignKey(() => Blog)
  @Column
  blog_id!: number

  @Comment('用户id')
  @AllowNull(false)
  @ForeignKey(() => User)
  @Column
  user_id!: number

  @Comment('内容')
  @AllowNull(false)
  @Column({ type: DataTypes.TEXT })
  content!: string

  @Comment('点赞数')
  @Default(0)
  @Column
  thumb_up?: number

  //需要查出此评论的用户信息用belongsto
  @BelongsTo(() => User)
  user?: User
}
export default Comments
